<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: PHP@妖孽 <dino_ma@163.com>
// +----------------------------------------------------------------------------

namespace Admin\Model;
/**
 * @todo 客服系统推荐管理模型
 * @since 2015-9-3
 */
class ServiceRecommendModel{

	public $error;//记录最后一次错误信息

	/**
	 * @todo 推荐管理筛选列表(我的老客户)
	 * @param 	activity_list array()  				活动信息二维数组
	 * 	<li>activity_id(int 11)</li>				活动id
	 * 	<li>ticket_level(tinyint 1)</li>			-1:全部; 0:普通票;1:VIP
	 * 	<li>is_signin(tinyint 1)</li>				-1:全部; 0:未签到;1:已签到
	 * 	<li>is_order(tinyint 1)</li>				-1:全部;0:未下单；1：已下单
	 * @param 	village_list	array() 		 	小区(id)信息一维数组
	 * @param 	demand_content_list	array()			需求内容id一维数组
	 * @param 	house_info	array() 				房产装修信息
	 * 	<li>min_decoration_meter(int 6)</li>		最少装修平米
	 * 	<li>max_decoration_meter(int 6)</li>		最多装修平米
	 * 	<li>min_decoration_budget(double,14,2)</li>	最少装修预算
	 * 	<li>max_decoration_budget(double,14,2)</li>	最多装修预算
	 * 	<li>min_home_time(date)</li>				最快进户时间
	 * 	<li>max_home_time(date)</li>				最慢进户时间
	 * 	<li>min_decoration_time(date)</li>			最快装修时间
	 * 	<li>max_decoration_time(date)</li>			最慢装修时间
	 * @param type int 								0取数,1取数据
	 * @author PHP@妖孽 <dino_ma@163.com>
	 * @since 2015-9-10
	 */
	public function getRecommendList($activity_list,$village_list,$demand_content_list,$house_info,$type=0){
		$activity_list = array_filter($activity_list);
		$village_list = array_unique(array_filter($village_list));
		$demand_content_list = array_filter($demand_content_list);
		$house_info = array_filter($house_info);
		$type = (int)$type;
		static $call_back = array();
		if(!in_array($type, array(0,1))){
			$call_back['status'] = 0;
			$call_back['info'] = 'type数据有问题';
			return $call_back;
		}
		
		$where = array();//初始化查询条件
		//做数据的比较验证
		$activity_ids = array();
		if(count($activity_list)>0){
			foreach ($activity_list as $activity_key =>$activity_val){
				if(!array_key_exists('activity_id', $activity_val) || $activity_val['activity_id']=='' || (int)$activity_val['activity_id']==0){
					unset($activity_list[$activity_key]);
				}
				if(!array_key_exists('ticket_level', $activity_val) || !in_array($activity_val['ticket_level'], array(-1,0,1))){
					unset($activity_list[$activity_key]);
				}
				if(!array_key_exists('is_signin', $activity_val) || !in_array($activity_val['is_signin'], array(-1,0,1))){
					unset($activity_list[$activity_key]);
				}
				if(!array_key_exists('is_order', $activity_val) || !in_array($activity_val['is_order'], array(-1,0,1))){
					unset($activity_list[$activity_key]);
				}
			}
			$activity_num = count($activity_list);
			if($activity_num>0){
				$activity_map =array();//初始化查询条件.
				foreach ($activity_list as $key=>$val){
					$activity_map[$key]['activity_id'] = $val['activity_id'];
					if(in_array($val['ticket_level'], array(0,1))){
						$activity_map[$key]['ticket_level'] = $val['ticket_level'];
					}
					
					if(in_array($val['is_signin'], array(0,1))){
						$activity_map[$key]['is_signin'] = $val['is_signin'];
					}
					if(in_array($val['is_order'], array(0,1))){
						$activity_map[$key]['is_order'] = $val['is_order'];
					}
				}
				$activity_map = array_merge($activity_map);
				if(empty($activity_map)){
					$call_back['status'] = 0;
					$call_back['info'] = '请选择活动';
					return $call_back;
				}
			}
		}
		if(empty($activity_list) && empty($village_list) && empty($demand_content_list) && empty($house_info)){
			$call_back['status'] = 0;
			$call_back['info'] = '请选择筛选条件';
			return $call_back;
		}
		//先获取我的所有的老客户.
		$my_customer_all = M('ServiceMyCustomer')->field('customer_id')->where(array( 'member_id' => UID))->select();
		if(empty($my_customer_all)){
			$call_back['status'] = 0;
			$call_back['info'] = '靓仔,美铝你也没有老客户阿, 找金哥给你分点';
			return $call_back;
		}
		$customer_id_all = array_column($my_customer_all, 'customer_id');
		if(empty($customer_id_all)){
			$call_back['status'] = 0;
			$call_back['info'] = '靓仔,美铝你也没有老客户阿, 找金哥给你分点';
			return $call_back;
		}
		//只保留有效的客户
		$valid_map['customer_id'] = array('in', $customer_id_all);
		$valid_map['is_valid'] = 0;
		$my_customer = M('ServiceCustomerDetail')->field('customer_id')->where($valid_map)->select();
		if(empty($my_customer)){
		    $call_back['status'] = 0;
		    $call_back['info'] = '靓仔,美铝你也没有有效用户阿, 找金哥给你分点';
		    return $call_back;
		}
		//只获取我的客户
		$customer_id = array_column($my_customer, 'customer_id');
		if(empty($customer_id)){
		    $call_back['status'] = 0;
		    $call_back['info'] = '靓仔,美铝你也没有有效用户阿, 找金哥给你分点';
		    return $call_back;
		}
		$where['customer_id'] = array('in', $customer_id);
		$where_member['member_id'] = UID;
		//装修信息
		if(!empty($house_info)){
			//装修平米
			if(array_key_exists('min_decoration_meter', $house_info) && array_key_exists('min_decoration_meter', $house_info)){
				if((is_null(['min_decoration_meter']) || is_null($house_info['max_decoration_meter']))){
					$call_back['status'] = 0;
					$call_back['info'] = '装修平米区间有误~';
					return $call_back;
				}elseif($house_info['min_decoration_meter'] > $house_info['max_decoration_meter']){
					$call_back['status'] = 0;
					$call_back['info'] = '装修平米区间有误~';
					return $call_back;
				}else {
					$where['decoration_meter'] = array('between',array($house_info['min_decoration_meter'],$house_info['max_decoration_meter']));
				}
			}
			//装修预算
			if(array_key_exists('min_decoration_budget', $house_info) && array_key_exists('max_decoration_budget', $house_info)){
				if((is_null(['min_decoration_budget']) || is_null($house_info['max_decoration_budget']))){
					$call_back['status'] = 0;
					$call_back['info'] = '装修预算有误~';
					return $call_back;
				}elseif($house_info['min_decoration_budget'] > $house_info['max_decoration_budget']){
					$call_back['status'] = 0;
					$call_back['info'] = '装修预算区间有误~';
					return $call_back;
				}else {
					$where['decoration_budget'] = array('between',array(doubleval($house_info['min_decoration_budget']),doubleval($house_info['max_decoration_budget'])));
				}
			}
			
			//入户时间
			if(array_key_exists('min_home_time', $house_info) && array_key_exists('max_home_time', $house_info)){
				if((is_null(['min_home_time']) || is_null($house_info['max_home_time']))){
					$call_back['status'] = 0;
					$call_back['info'] = '入户时间有误~';
					return $call_back;
				}elseif($house_info['min_home_time'] > $house_info['max_home_time']){
					$call_back['status'] = 0;
					$call_back['info'] = '入户区间有误~';
					return $call_back;
				}else {
					$where['home_time'] = array('between',array($house_info['min_home_time'],$house_info['max_home_time']));
				}
			}
			
			//装修时间
			if(array_key_exists('min_decoration_time', $house_info) && array_key_exists('max_decoration_time', $house_info)){
				if((is_null(['min_decoration_time']) || is_null($house_info['max_decoration_time']))){
					$call_back['status'] = 0;
					$call_back['info'] = '装修时间有误~';
					return $call_back;
				}elseif($house_info['min_decoration_time'] > $house_info['max_decoration_time']){
					$call_back['status'] = 0;
					$call_back['info'] = '装修区间有误~';
					return $call_back;
				}else {
					$where['decoration_time'] = array('between',array($house_info['min_decoration_time'],$house_info['max_decoration_time']));
				}
			}
		}
		
		//
		if(!empty($village_list)){
			$where['village_id'] = array('in',$village_list);
		}
		
		if($demand_content_list){
			$demand_content_id=array();
			foreach ($demand_content_list as $key=>$val){
				foreach ($val as $k=>$v){
					array_push($demand_content_id, $v);
				}
			}
			if(!empty($demand_content_id)){
				$where['demand_content_id'] = array('in', array_merge($demand_content_id));
			}
		}
		
		$db_pre = C('DB_PREFIX');
		$field = 'customer_id';
		//case 1
		//只选择活动
		if(!empty($activity_list) && empty($village_list) && empty($demand_content_id) && empty($house_info)){
			switch ($type){
				case 0:
				case 1:
						$list = $this->getActivityCustoemr($activity_num,$activity_map, $where, $field);
						$call_back['status'] = 1;
						$call_back['data']['num'] = count($list);
						$call_back['data']['list'] = (array)$list;
						return $call_back;
					break;
				default:
					$call_back['status'] = 0;
					$call_back['info'] = 'type有异常';
					return $call_back;
					break;
			}
		}
		
		//case 2
		//只选择小区房产信息
		if(empty($activity_list) && empty($demand_content_id) && (!empty($village_list) ||!empty($house_info))){
			switch ($type){
				case 0:
				case 1:
						$list = $this->getHouseCustomer($where, $field);
						$call_back['status'] = 1;
						$call_back['data']['num'] = count($list);
						$call_back['data']['list'] =$list;
						return $call_back;
					break;
				default:
					$call_back['status'] = 0;
					$call_back['info'] = 'type有异常';
					return $call_back;
					break;
			}
		}
		
		//case 3
		//只选择需求信息
		if(empty($activity_list) && empty($village_list) && !empty($demand_content_id) && empty($house_info)){
			if(empty($demand_content_id)){
				$call_back['status'] = 0;
				$call_back['info'] = '需求数据有异常';
				return $call_back;
			}
			switch ($type){
				case 0:
				case 1:
						$list = $this->getDemandCustomer($where,$field);
						$call_back['status'] = 1;
						$call_back['data']['num'] = count($list);
						$call_back['data']['list'] = $list;
						return $call_back;
					break;
				default:
					$call_back['status'] = 0;
					$call_back['info'] = 'type有异常';
					return $call_back;
					break;
			}
		}
		
		//case 4
		//活动 房产信息(小区装修信息)
		if(!empty($activity_list) && (!empty($village_list) || !empty($house_info))&& empty($demand_content_id) ){
			switch ($type){
				case 0:
				case 1:
						$activity_customer_id_list = $this->getActivityCustoemr($activity_num, $activity_map, $where, $field);
						if(empty($activity_customer_id_list)){
							$call_back['status'] = 1;
							$call_back['data']['num'] = 0;
							$call_back['data']['list'] =array();
							return $call_back;
						}
						$house_map = array();
						$house_map = $where;
						$house_map['customer_id'] = array('in',$activity_customer_id_list);
						$list = $this->getHouseCustomer($house_map, $field);
						$call_back['status'] = 1;
						$call_back['data']['num'] = count($list);
						$call_back['data']['list'] = $list;
						return $call_back;
					break;
				default:
					$call_back['status'] = 0;
					$call_back['info'] = 'type有异常';
					return $call_back;
					break;
			}
		}
		//case 5
		//活动和需求(用户tag)
		if(!empty($activity_list) && empty($village_list) && empty($house_info)&& !empty($demand_content_id) ){
			switch ($type){
				case 0:
				case 1:
					$activity_customer_id_list = $this->getActivityCustoemr($activity_num, $activity_map, $where, $field);
					if(empty($activity_customer_id_list)){
						$call_back['status'] = 1;
						$call_back['data']['num'] = 0;
						$call_back['data']['list'] =array();
						return $call_back;
					}
					$demand_map = array();
					$demand_map = $where;
					$demand_map['customer_id'] = array('in',$activity_customer_id_list);
					$list = $this->getDemandCustomer($demand_map, $field);
					$call_back['status'] = 1;
					$call_back['data']['num'] = count($list);
					$call_back['data']['list'] = $list;
					return $call_back;
					break;
				default:
					$call_back['status'] = 0;
					$call_back['info'] = 'type有异常';
					return $call_back;
					break;
			}
		}
		//case 6
		// 房产信息(小区装修) 用户需求
		if(empty($activity_list) && (!empty($village_list) || !empty($house_info)) && !empty($demand_content_id) ){
			switch ($type){
				case 0:
				case 1:
						$house_map = array();//初始化查询条件
						$house_map['customer_id'] = $where['customer_id'];
						if(isset($where['decoration_meter'])){
							$house_map['decoration_meter'] = $where['decoration_meter'];
						}
						if(isset($where['decoration_budget'])){
							$house_map['decoration_budget'] = $where['decoration_budget'];
						}
						if(isset($where['home_time'])){
							$house_map['home_time'] = $where['home_time'];
						}
						if(isset($where['decoration_time'])){
							$house_map['decoration_time'] = $where['decoration_time'];
						}
						if(isset($where['village_id'])){
							$house_map['village_id'] = $where['village_id'];
						}
						
						$house_list_customer_ids = $this->getHouseCustomer($house_map, $field);
						if(empty($house_list_customer_ids)){
							$call_back['status'] = 1;
							$call_back['data']['num'] = 0;
							$call_back['data']['list'] =array();
							return $call_back;
						}
						$demand_map = array();
						$demand_map['demand_content_id'] = $where['demand_content_id'];
						$demand_map['customer_id'] = array('in',$house_list_customer_ids);
						$demand_list_customer_ids = $this->getDemandCustomer($demand_map, $field);
						$call_back['status'] = 1;
						$call_back['data']['num'] = count($demand_list_customer_ids);
						$call_back['data']['list'] =$demand_list_customer_ids;
						return $call_back;
					break;
				default:
					$call_back['status'] = 0;
					$call_back['info'] = 'type有异常';
					return $call_back;
					break;
			}
		}
		//case7
		//活动 房产信息(小区装修信息) 用户需求
		if(!empty($activity_list) && !empty($village_list) && !empty($house_info)&& !empty($demand_content_id) ){
			switch ($type){
				case 0:
				case 1:
						$activity_customer_id_list = $this->getActivityCustoemr($activity_num, $activity_map, $where, $field);
						if(empty($activity_customer_id_list)){
							$call_back['status'] = 1;
							$call_back['data']['num'] = 0;
							$call_back['data']['list'] =array();
							return $call_back;
						}
						$house_map = array();//初始化查询条件
						$house_map['customer_id'] = $activity_customer_id_list;
						if(isset($where['decoration_meter'])){
							$house_map['decoration_meter'] = $where['decoration_meter'];
						}
						if(isset($where['decoration_budget'])){
							$house_map['decoration_budget'] = $where['decoration_budget'];
						}
						if(isset($where['home_time'])){
							$house_map['home_time'] = $where['home_time'];
						}
						if(isset($where['decoration_time'])){
							$house_map['decoration_time'] = $where['decoration_time'];
						}
						if(isset($where['village_id'])){
							$house_map['village_id'] = $where['village_id'];
						}
						
						$house_list_customer_ids = $this->getHouseCustomer($house_map, $field);
						if(empty($house_list_customer_ids)){
							$call_back['status'] = 1;
							$call_back['data']['num'] = 0;
							$call_back['data']['list'] =array();
							return $call_back;
						}
						$demand_map = array();
						$demand_map['demand_content_id'] = $where['demand_content_id'];
						$demand_map['customer_id'] = array('in',$house_list_customer_ids);
						$demand_list_customer_ids = $this->getDemandCustomer($demand_map, $field);
						$call_back['status'] = 1;
						$call_back['data']['num'] = count($demand_list_customer_ids);
						$call_back['data']['list'] =$demand_list_customer_ids;
						return $call_back;
					break;
				default:
					$call_back['status'] = 0;
					$call_back['info'] = 'type有异常';
					return $call_back;
					break;
			}
		}
	}
	
	/**
	 * @todo  获取活动下的用户
	 */
	private function getActivityCustoemr($activity_num,$activity_map,$where,$field){
		$customer_list = array();
		$obj = M('service_activity_join');
		$customer_id_list = array();
		$map = array();
		//循环查询数据库
		for ($i=0;$i<$activity_num;$i++){
			$map = $where+$activity_map[$i];
			if ($info = $obj->field($field)->where($map)->select()){
				$customer_id_list+=$info;
			}
			usleep(100);
		}
		
		$customer_list = array_unique(array_column($customer_id_list, $field));
		return $customer_list;
	}
	
	/**
	 * @todo 获取房产信息的用户
	 */
	private function getHouseCustomer($where,$field){
		$customer_list = array();
		$obj = M('service_customer_house');
		$list = array();
		$list =  (array)$obj->field($field)->where($where)->select();
		$customer_list = array_unique(array_column($list, $field));
		return $customer_list;
	}
	
	/**
	 * @todo 获取需求上的用户
	 */
	private function getDemandCustomer($where,$field){
		$customer_list = array();
		$obj = M('service_customer_demand');
		$list =array();
		$list = (array)$obj->field($field)->where($where)->select();
		$customer_list = array_unique(array_column($list, $field));
		return $customer_list;
	}
	
	/**
	 * @todo 创建我的推荐列表
	 * @param string recommend_name(1,2) 推荐列表名称
	 * @param string customer_ids 用户List
	 */
	public function creatMyRecommend($recommend_name,$customer_ids){
		static $call_back = array();
		if(strlen($recommend_name) <1 || strlen($recommend_name)>21){
			$call_back['status'] = 0;
			$call_back['info'] = '推荐列表名称需要在1-20个字符~';
			return $call_back;
		}
		if(!is_array($customer_ids)|| empty($customer_ids)){
			$call_back['status'] = 0;
			$call_back['info'] = '您没有可推荐的人员~';
			return $call_back;
		}
		
		$data = array('recommend_name'=>$recommend_name,'member_id'=>UID,'create_time'=>NOW_TIME);
		$id = M('service_recommend')->add($data);
		if(!$id){
			$call_back['status'] = 0;
			$call_back['info'] = '创建推荐列表失败';
			return $call_back;
		}
		$customer_ids = array_values($customer_ids);
		$length = count($customer_ids);
		for ($i=0;$i<$length;$i++){
			$data_list[$i]['recommend_id'] =$id;
			$data_list[$i]['customer_id'] = $customer_ids[$i];
			$data_list[$i]['is_valid'] = 0;
			$data_list[$i]['is_call'] = 0;
			$data_list[$i]['member_id'] = UID;
			$data_list[$i]['later_call_time'] = 0;
		}
		if(M('service_recommend_customer')->addAll($data_list)){
		    action_log('recommend_add', 'service_recommend', $id, UID);
			$call_back['status'] = 1;
			$call_back['info'] = '操作成功';
		}else{
			$call_back['status'] = 0;
			$call_back['info'] = '操作失败';
		}
		return $call_back;
	}
	
	/**
	 * @todo 返回最后一次错误信息
	 * @return string
	 * @author PHP@妖孽 <dino_ma@163.com>
	 */
	public function getError(){
		return $this->error;
	}
}
